package com.tang.Web;

import com.tang.pojo.Page;
import com.tang.service.PageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientSevlet extends BaseServlet{
    protected void PageManage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNowStr=req.getParameter("pageNow");
        String pageSizeStr = req.getParameter("pageSize");
        String begin=req.getParameter("begin");
        String end=req.getParameter("end");
        PageServiceImpl psi = new PageServiceImpl();
        if(pageNowStr==null||pageNowStr.equals("")||pageSizeStr==null||pageSizeStr.equals("")){
            if(begin==null||begin.equals("")||end==null||end.equals("")) {
                Page page = psi.getPageByPrice(1, 4, 0, Integer.MAX_VALUE);
                req.setAttribute("bookss", page.getItems());
                req.setAttribute("pages", page);
            }else{
                int beginI=Integer.parseInt(begin);
                int endI=Integer.parseInt(end);
                Page page = psi.getPageByPrice(1, 4, beginI, endI);
                req.setAttribute("bookss", page.getItems());
                req.setAttribute("pages", page);
            }
            req.getRequestDispatcher("pages/StoreIndex.jsp").forward(req, resp);
        }else {
            int pageNow = Integer.parseInt(pageNowStr);
            int pageSize = Integer.parseInt(pageSizeStr);
            if(begin==null||begin.equals("")||end==null||end.equals("")) {
                Page page = psi.getPageByPrice(pageNow, pageSize, 0, Integer.MAX_VALUE);
                if (pageNow < 1 || pageNow > page.getPageTotal()) {
                    System.out.println("无效输入");
                } else {
                    req.setAttribute("bookss", page.getItems());
                    req.setAttribute("pages", page);
                }
            }else{
                int beginI=Integer.parseInt(begin);
                int endI=Integer.parseInt(end);
                Page page = psi.getPageByPrice(pageNow, pageSize, beginI, endI);
                if (pageNow < 1 || pageNow > page.getPageTotal()) {
                    System.out.println("无效输入");
                } else {
                    req.setAttribute("bookss", page.getItems());
                    req.setAttribute("pages", page);
                }
            }
            req.getRequestDispatcher("pages/StoreIndex.jsp").forward(req, resp);
        }
    }
    protected void PageManageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNowStr=req.getParameter("pageNow");
        String pageSizeStr = req.getParameter("pageSize");
        String begin=req.getParameter("begin");
        String end=req.getParameter("end");
        PageServiceImpl psi = new PageServiceImpl();
        if(pageNowStr==null||pageNowStr.equals("")||pageSizeStr==null||pageSizeStr.equals("")){
            if(begin==null||begin.equals("")||end==null||end.equals("")) {
                Page page = psi.getPageByPrice(1, 4, 0, Integer.MAX_VALUE);
                req.setAttribute("bookss", page.getItems());
                req.setAttribute("pages", page);
            }else{
                int beginI=Integer.parseInt(begin);
                int endI=Integer.parseInt(end);
                Page page = psi.getPageByPrice(1, 4, beginI, endI);
                req.setAttribute("bookss", page.getItems());
                req.setAttribute("pages", page);
            }
            req.getRequestDispatcher("pages/StoreIndex.jsp").forward(req, resp);
        }else {
            int pageNow = Integer.parseInt(pageNowStr);
            int pageSize = Integer.parseInt(pageSizeStr);
            if(begin==null||begin.equals("")||end==null||end.equals("")) {
                Page page = psi.getPageByPrice(pageNow, pageSize, 0, Integer.MAX_VALUE);
                if (pageNow < 1 || pageNow > page.getPageTotal()) {
                    System.out.println("无效输入");
                } else {
                    req.setAttribute("bookss", page.getItems());
                    req.setAttribute("pages", page);
                }
            }else{
                int beginI=Integer.parseInt(begin);
                int endI=Integer.parseInt(end);
                Page page = psi.getPageByPrice(pageNow, pageSize, beginI, endI);
                if (pageNow < 1 || pageNow > page.getPageTotal()) {
                    System.out.println("无效输入");
                } else {
                    req.setAttribute("bookss", page.getItems());
                    req.setAttribute("pages", page);
                }
            }
            req.getRequestDispatcher("pages/StoreIndex.jsp").forward(req, resp);
        }
    }
}
